package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tanhua.common.mapper.SettingsMapper;
import com.tanhua.common.pojo.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Service 通用设置
 *
 * @author zZZFf
 * @since 2021/7/11 14:49
 */
@Service
public class SettingsService {
    @Autowired
    private SettingsMapper settingsMapper;

    /**
     * 根据用户id查询用户通用设置
     *
     * @param userId 用户id
     * @return Settings
     */
    public Settings findByUserId(Long userId) {
        QueryWrapper<Settings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);

        return this.settingsMapper.selectOne(queryWrapper);
    }

    /**
     * 添加用户通用设置
     *
     * @param settings 通用设置对象
     * @return Boolean
     */
    public Boolean saveSettings(Settings settings) {
        return this.settingsMapper.insert(settings) == 1;
    }

    /**
     * 修改用户通用设置
     *
     * @param settings 通用设置对象
     * @return Boolean
     */
    public Boolean updateSettings(Settings settings) {
        UpdateWrapper<Settings> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", settings.getUserId());

        return this.settingsMapper.update(settings, updateWrapper) == 1;
    }
}
